<?php
class site extends MySQL_Connector {
	public $log;
	public $rangs;
	
	private $bool = true;
	
	private $table_attrs = "";
	private $row_header_attrs = "";
	private $row_1_attrs = "";
	private $row_2_attrs = "";
	private $tr_class = "";
	
	private $db_columns = array();
	
	public function __construct(Array $db_columns) {
		parent::__construct();
		$this->log = new log();
		$this->mode_types = new site_mode_type();
		$this->rangs = general::get_rangs();
		$this->db_columns = $db_columns;
	}
	
	public function __destruct() {
		parent::__destruct();
	}
	
	private $mode_edit = NULL;
	private $mode_new = NULL;
	private $mode_show = NULL;
	private $mode_types;
	
	public function add_mode(site_mode $mode) {
		switch($mode->mode_type) {
			case "new":
				$this->mode_new = $mode;
				break;
			case "edit":
				$this->mode_edit = $mode;
				break;
			case "show":
				$this->mode_show = $mode;
				break;
		}
	}
	
	public function set_attributes_for_table($attr_table, $attr_header_row, $attr_row_1, $attr_row_2) {
		$this->table_attrs = $attr_table;
		$this->row_header_attrs = $attr_header_row;
		$this->row_1_attrs = $attr_row_1;
		$this->row_2_attrs = $attr_row_2; 
	}
	
	private function get_header_row($active, $header_cols, $action_active) {
		if(!$active) {
			return "";
		}
		$row = "";
		$row .= "<tr " . $this->row_header_attrs . ">";
		foreach($header_cols as $value) {
			$row .= "<td>".$value."</td>";
		}
		if($action_active) {
			$row .= "<td>Aktion</td>";
		}
		$row .= "</tr>";
		return $row;
	}
	
	private function get_row($action_active, Array $data_row) {
		$row = "<tr ".$this->tr_class.">";
		foreach($this->db_columns as $var) {
			$row .= "<td valign=\"middle\">".$data_row[$var]."</td>";
		}
		if($action_active) {
			if($this->mode_delete) {
				$this->action_label .= "<a href=\"".$this->mode_delete_action.$data_row["id"]."\">".$this->mode_delete_img."</a>";
			}
			$row .= "<td>".$this->action_label."</td>";
		}
		$row .= "</tr>";
		return $row;
	}
	
	private $mode_delete = false;
	private $mode_delete_action = "";
	private $mode_delete_img = "";
	
	public function add_mode_delete($action_active, $action_img, $action) {
		$this->mode_delete = $action_active;
		if($action_active) {
			$this->mode_delete_img = $action_img;
			if(preg_match("/(\?)/", $action)) {
				$action .= "&id=";
				$this->mode_delete_action = $action;
			} else {
				$action .= "?id=";
				$this->mode_delete_action = $action;
			}
			$this->mode_delete_img = $action_img;
		}
	}
	
	public function are_modes_not_null() {
		if($this->mode_edit != NULL && $this->mode_new != NULL && $this->mode_show != NULL) {
			return false;
		}
		return true;
	}
	
	public $action_label = "";
	
	public function create_table($sql, $show_header, $show_footer, Array $columns_header, $row_indexer) {
		$table = "";
		$result = $this -> con -> query($sql);
		if($result) {
			$table .= "<table " . $this->table_attrs . ">";
			$table .= $this->get_header_row($show_header, $columns_header, true);

			while($row = mysqli_fetch_array($result)) {
				$this->tr_class = $this->get_tr_class();
				$this->action_label = "<a href=\"index.php?page=allgemein&".$this->mode_edit->mode_get_var_name."=edit&".$this->mode_edit->indexer."=".$row[$this->mode_edit->indexer]."\"><img src=\"../img/note_edit.png\" width=\"16px\" height=\"16px\" /></a>";
				
				if(isset($_GET[$this->mode_edit->mode_get_var_name], $_GET[$this->mode_show->indexer])) {
					if($_GET[$this->mode_edit->mode_get_var_name] == "edit") {
						$this->mode_edit->set_datarow($row);
						$table .= $this->get_edit_row();
					} else {
						$table .= $this->get_row(true, $row);
					}
				} else {
					$table .= $this->get_row(true, $row);
				}
			}
			$table .= $this->get_new_row();
			if($show_footer) {
				$label = $this->get_new_label();
				$table .= "<tr ".$this->row_header_attrs."><td colspan=\"".(count($this->db_columns)+1)."\">".$label."</td><tr>";
			}
			$table .= "</table>";
		}
		return $table;
	}
	
	private function get_new_label() {
		if($this->mode_new != NULL) {
			return "<a href=\"".$this->mode_new->mode_activation_url."\"><img src=\"".general::get_url(2, $this->mode_new->mode_btn_icon)."\" width=\"16px\" height=\"16px\" /></a>";
		}
		return "";
	}
	
	public function get_edit_row() {
		$row = "";
		if($this->mode_edit != NULL) {
			if(isset($_GET[$this->mode_edit->mode_get_var_name])) {
				if($_GET[$this->mode_edit->mode_get_var_name] == $this->mode_edit->mode_type) {
					if($this->mode_edit->datarow[$this->mode_edit->indexer] == $_GET[$this->mode_edit->indexer]) {
						$row .= "<form action=\"".$this->mode_edit->mode_standard_url."\" method=\"".$this->mode_edit->mode_form_method."\"><tr ".$this->tr_class.">";
						$id = $this->mode_edit->datarow[$this->mode_edit->indexer];
						foreach($this->mode_edit->column_structur as $key => $value) {
							$row .= "<td valign=\"middle\"><input type=\"text\" name=\"".$key."\" value=\"".$this->mode_edit->datarow[$value]."\" /></td>";
						}
						$row .= "<td><input type=\"hidden\" name=\"".$this->mode_edit->indexer."\" value=\"".$id."\" /><input type=\"hidden\" name=\"".$this->mode_edit->mode_submit_name."\" value=\"".$this->mode_edit->mode_submit_value."\" /><input type=\"image\" name=\"send\" src=\"".general::get_url(2, $this->mode_edit->mode_submit_img)."\" width=\"16px\" height=\"16px\" /></td>";
						$row .= "</tr>";
						$row .= "</form>";
						return $row;
					}
					
				}
			}
		}
		return $this->get_row(true, $this->mode_edit->datarow);
	}
	
	public function get_new_row() {
		$row = "";
		if($this->mode_new != NULL) {
			if(isset($_GET[$this->mode_new->mode_get_var_name])) {
				if($_GET[$this->mode_new->mode_get_var_name] == $this->mode_new->mode_type) {
					$this->tr_class = $this->get_tr_class();
					$row .= "<form action=\"".$this->mode_new->mode_standard_url."\" method=\"".$this->mode_new->mode_form_method."\"><tr ".$this->tr_class.">";
					foreach($this->mode_new->column_structur as $key => $value) {
						$row .= "<td valign=\"middle\"><input type=\"text\" name=\"".$key."\" value=\"\" /></td>";
					}
					$row .= "<td><input type=\"hidden\" name=\"".$this->mode_new->mode_submit_name."\" value=\"".$this->mode_new->mode_submit_value."\" /><input type=\"image\" name=\"send\" src=\"".general::get_url(2, $this->mode_new->mode_submit_img)."\" width=\"16px\" height=\"16px\" /></td>";
					$row .= "</tr></form>";
				}
			}
		}
		return $row;
	}
	
	/*
	 * Speichert eine Seite nach dem Bearbeiten.
	 */
	public function save_site($datatable_name) {
		if(array_key_exists($_POST["rang"], $this->rangs)) {
			$sql = "UPDATE ".$datatable_name." SET ";
			$set = "";
			for($i = 0; $i < count($this->db_columns); $i++) {
				if($i == (count($this->db_columns)-1)) {
					$set .= $this->db_columns[$i]."='".$_POST[$this->db_columns[$i]]."'";
				} else {
					$set .= $this->db_columns[$i]."='".$_POST[$this->db_columns[$i]]."', ";
				}
			}
			$sql .= $set." WHERE id=".$_POST["id"];
			if($this -> con -> query($sql)) {
				$this->log->add_log_entry(new log_entry("Der Seite wurde erfolgreich ge&auml;ndert!", "success"));
			} else {
				$this->log->add_log_entry(new log_entry("Die Seite konnte nicht bearbeitet werden!", "error"));
			}
		} else {
			$this->log->add_log_entry(new log_entry("Der Rang \"".$_POST["rang"]."\" existiert nicht!", "error"));
		}
	}
	
	/*
	 * Fügt in der Datenbank eine neue Zeile hinzu.
	 */
	public function add_site($datatable_name) {
		if(array_key_exists($_POST["rang"], $this->rangs)) {
			$sql = "INSERT INTO ".$datatable_name." ";
			$cols = "(";
			$values = "VALUES(";
			for($i = 0; $i < count($this->db_columns); $i++) {
				if($i == (count($this->db_columns)-1)) {
					$cols .= "`".$this->db_columns[$i]."`) ";
					$values .= "'".$_POST[$this->db_columns[$i]]."')";
				} else {
					$cols .= "`".$this->db_columns[$i]."`, ";
					$values .= "'".$_POST[$this->db_columns[$i]]."', ";
				}
			}
			$sql .= $cols.$values;
			if($this -> con -> query($sql)) {
				$this->log->add_log_entry(new log_entry("Der Seite wurde erfolgreich erstellt!", "success"));
			} else {
				$this->log->add_log_entry(new log_entry("Die Seite konnte nicht bearbeitet werden!", "error"));
			}
		} else {
			$this->log->add_log_entry(new log_entry("Der Rang \"".$_POST["rang"]."\" existiert nicht!", "error"));
		}
	}
	
	/*
	 * Seite löschen
	 */
	public function delete_site($datatable_name, $db_index, $db_index_value) {
		$sql = "DELETE FROM ".$datatable_name." WHERE ".$db_index."=".$db_index_value;
		if($this -> con -> query($sql)) {
			$this->log->add_log_entry(new log_entry("Der Seite wurde erfolgreich gel&ouml;scht!", "success"));
		} else {
			$this->log->add_log_entry(new log_entry("Die Seite konnte nicht gel&ouml;scht werden!", "error"));
		}
	}
	
	private function get_tr_class() {
		if ($this->bool) {
			$tr_class = $this->row_1_attrs;
		} else {
			$tr_class = $this->row_2_attrs;
		}
		$this->bool = !$this->bool;
		return $tr_class;
	}
	
	private function call_function($functionname)
	{
		$this->{$functionname}();
	}
}
?>